package Com.Ring.TradingMobile.Service;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import Com.Ring.TradingMobile.Object.CompanyInfo;
import Com.Ring.TradingMobile.Object.MarketInfo;
import Com.Ring.TradingMobile.Object.StockInfo;
import android.text.format.DateFormat;
import android.util.Log;

public class WebService {
	
	private String NAMESPACE = "http://tempuri.org/";
	private String ADDRESS_SERVICE = "http://118.70.241.136/RTServiceTest/Service.asmx";
    private static final String GetListStockInfos = "GetListStockInfos";
    private static final String GetListCompany = "GetListCompany";
    private static final String GetMarketInfo = "GetMarketInfo";
    
    private String TAG = "WebService";
	public WebService() {
		
	}
	
	public MarketInfo GetMarketInfoAndStatus (int marketId) {
		SoapObject request = new SoapObject(NAMESPACE, GetMarketInfo);
		SoapSerializationEnvelope envelope =new SoapSerializationEnvelope(SoapEnvelope.VER11);
		PropertyInfo paraMarketId = new PropertyInfo();
		paraMarketId.setName("marketID");
		paraMarketId.setValue(marketId);
		paraMarketId.setType(int.class);
		paraMarketId.setNamespace(NAMESPACE);
		request.addProperty(paraMarketId);
		envelope.setOutputSoapObject(request);
		Log.d(TAG, request.toString());
		HttpTransportSE http =new HttpTransportSE(ADDRESS_SERVICE);
		String soapAction = NAMESPACE+GetMarketInfo;
		try {
			http.call(soapAction, envelope);
			JSONObject responJson = new JSONObject(envelope.getResponse().toString());
			JSONArray objArray = responJson.getJSONArray("Result");
			JSONObject obj = objArray.getJSONObject(0);
			MarketInfo marketInfo = new MarketInfo();
			marketInfo.marketid = obj.getInt(MarketInfo.MARKET_ID);
			//marketInfo.CountFloor = Float.parseFloat(obj.getString(MarketInfo.COUNT_FLOOR));
			marketInfo.IndexChanged = Float.parseFloat(obj.getString(MarketInfo.INDEX_CHANGED));
			marketInfo.SetIndex = Float.parseFloat(obj.getString(MarketInfo.SET_INDEX));
			marketInfo.Status = obj.getString(MarketInfo.STATUS);
			marketInfo.Time = obj.getInt(MarketInfo.TIME);
			marketInfo.TotalShares = Float.parseFloat(obj.getString(MarketInfo.TOTAL_SHARES));
			marketInfo.TotalValues = Float.parseFloat(obj.getString(MarketInfo.TOTAL_VALUES));
			marketInfo.TradeDate = obj.getString(MarketInfo.TRADE_DATE);
			if(!marketInfo.TradeDate.equals("")) {
				marketInfo.TradeDate = marketInfo.TradeDate.substring(6, marketInfo.TradeDate.length()-2);
				marketInfo.TradeDate = DateFormat.format("yyyy-MM-dd", Long.parseLong(marketInfo.TradeDate)).toString();
				Log.d(TAG, marketInfo.TradeDate);
			}
			return marketInfo;
		}catch(Exception ex) {
			Log.d(TAG, ex.toString());
			return null;
		}
	}
	
	public List<CompanyInfo> GetCompanyList(int marketId, int langId){
		SoapObject request = new SoapObject(NAMESPACE, GetListCompany);
		SoapSerializationEnvelope envelope =new SoapSerializationEnvelope(SoapEnvelope.VER11);
		PropertyInfo paraMarketId = new PropertyInfo();
		paraMarketId.setName("marketID");
		paraMarketId.setValue(marketId);
		paraMarketId.setType(int.class);
		PropertyInfo paraLangId = new PropertyInfo();
		paraLangId.setName("languageId");
		paraLangId.setValue(langId);
		paraLangId.setType(int.class);
		request.addProperty(paraMarketId);
		request.addProperty(paraLangId);
		envelope.setOutputSoapObject(request);
		Log.d(TAG, request.toString());
		HttpTransportSE http =new HttpTransportSE(ADDRESS_SERVICE);
		String soapAction = NAMESPACE+GetListCompany;
		try {
			http.call(soapAction, envelope);
			JSONObject responJson = new JSONObject(envelope.getResponse().toString());
			JSONArray companyArray = responJson.getJSONArray("Result");
			List<CompanyInfo> companyInfoList = new ArrayList<CompanyInfo>(); 
			for(int i=0; i<companyArray.length(); i++) {
				JSONObject obj = companyArray.getJSONObject(i);
				CompanyInfo company = new CompanyInfo();
				company.Code = obj.getString(CompanyInfo.CODE);
				company.MarketId = obj.getInt(CompanyInfo.MARKETID);
				company.FullName = obj.getString(CompanyInfo.FULLNAME);
				companyInfoList.add(company);
			}
			return companyInfoList;
		}catch(Exception ex) {
			Log.d(TAG, ex.toString());
			return null;
		}
	}
	
	public List<StockInfo> GetListStockInfo(String listCode, int langId){
		SoapObject request = new SoapObject(NAMESPACE, GetListStockInfos);
		SoapSerializationEnvelope envelope =new SoapSerializationEnvelope(SoapEnvelope.VER11);
		
		PropertyInfo paraListCode = new PropertyInfo();
		paraListCode.setName("listCodes");
		paraListCode.setValue(listCode);
		paraListCode.setNamespace(NAMESPACE);
		PropertyInfo paraLangId = new PropertyInfo();
		paraLangId.setNamespace(NAMESPACE);
		paraLangId.setName("languageId");
		paraLangId.setValue(String.valueOf(langId));
		
		request.addProperty(paraListCode);
		request.addProperty(paraLangId);
		envelope.setOutputSoapObject(request);
		HttpTransportSE http =new HttpTransportSE(ADDRESS_SERVICE);
		String soapAction = NAMESPACE+GetListStockInfos;
		try {
			http.call(soapAction, envelope);
			JSONObject responJson = new JSONObject(envelope.getResponse().toString());
			JSONArray stockArray = responJson.getJSONArray("Result");
			List<StockInfo> stockInfoList = new ArrayList<StockInfo>(); 
			for(int i=0; i<stockArray.length(); i++) {
				JSONObject obj = stockArray.getJSONObject(i);
				StockInfo stock = new StockInfo();
				stock.Symbol = obj.getString(StockInfo.SYMBOL);
				stock.RefPrice = Float.parseFloat(obj.getString(StockInfo.REF_PRICE));
				stock.CeilPrice = Float.parseFloat(obj.getString(StockInfo.CEIL_PRICE));
				stock.FloorPrice = Float.parseFloat(obj.getString(StockInfo.FLOOR_PRICE));
				stock.PriceMatch = Float.parseFloat(obj.getString(StockInfo.PRICE_MATCH));
				stock.Changed = Float.parseFloat(obj.getString(StockInfo.CHANGED));
				stock.PerChanged = Float.parseFloat(obj.getString(StockInfo.PER_CHANGED));
				stock.VolumeMatch = Float.parseFloat(obj.getString(StockInfo.VOLUME_MATCH));
				stock.TotalMatch = Float.parseFloat(obj.getString(StockInfo.TOTAL_MATCH));

				stock.BestPriceBuy = Float.parseFloat(obj.getString(StockInfo.BEST_PRICE_BUY));
				stock.BestVolbuy = Float.parseFloat(obj.getString(StockInfo.BEST_VOL_BUY));
				stock.BestPriceSell = Float.parseFloat(obj.getString(StockInfo.BEST_PRICE_SELL));
				stock.BestVolSell = Float.parseFloat(obj.getString(StockInfo.BEST_VOL_SELL));
				stock.TradeDate = obj.getString(StockInfo.TRADE_DATE);
				if(!stock.TradeDate.equals("")) {
					stock.TradeDate = stock.TradeDate.substring(6, stock.TradeDate.length()-2);
					stock.TradeDate = DateFormat.format("yyyy-MM-dd", Long.parseLong(stock.TradeDate)).toString();
					Log.d(TAG, stock.TradeDate);
				}
				stockInfoList.add(stock);
			}
			return stockInfoList;
		}catch(Exception ex) {
			Log.d(TAG, ex.toString()+"-"+listCode);
			return null;
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
